/*
 * Log4JPlugin.java
 *
 * Created on 15 de diciembre de 2005, 22:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.fiel.struts.actions;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import org.apache.log4j.PropertyConfigurator;

import org.fiel.FielConstants;

/**
 * @author Luis Pablo Gallo (luispablo.gallo@gmail.com)
 */
public class Log4JPlugin implements PlugIn{
    
    public void init(ActionServlet servlet, ModuleConfig config)
    throws ServletException {
        try{
            String file = FielConstants.LOG4J_PROPERTIES_FILE;
            String prefix = servlet.getServletContext().getRealPath("/WEB-INF/");
            
            Date today = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            
            System.setProperty(FielConstants.LOG4J_SYSTEM_PROPERTY, prefix + "/logs/");
            System.setProperty(FielConstants.DATE_SYSTEM_PROPERTY, format.format(today));
            
            PropertyConfigurator.configureAndWatch(prefix +"/"+ file);
            
        }catch (Exception ex){
            ex.printStackTrace();
            throw new ServletException(ex);
        }
    }
    
    public void destroy() {
    }
    
}
